//
//  ClinicalCollections.m
//  ClinicalCollections
//
//  Created by janakiraman gopinath on 12/28/12.
//  Copyright (c) 2012 GOPI. All rights reserved.
//

#import "ClinicalCollections.h"

@implementation ClinicalCollections

+ (NSDictionary *)clinicalCollectionsSummaryFetchWithlimit:(int)limit
{
    NSString *url;
    
    url = [NSString stringWithFormat:@"%@%d", API_SUMMARY_URL, (limit == -1) ? DEFAULT_FETCH_LIMIT:limit];
    
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSData *jsonData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:nil] dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    
    NSDictionary *clinicalCollections = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error] : nil;
    
    return clinicalCollections;
}

//
// Recursive function to get the node from the JSON object
//
+ (NSDictionary *) findNodeWithKey:(NSString *) key forArray:(id) array
{
    if ([array isKindOfClass:[NSDictionary class]])
    {
        NSArray *allKeys = [array allKeys];
        for(int j = 0; j < allKeys.count;j++) {
            if([allKeys[j] isEqual:key])
                return [array objectForKey:key];
        }
        return nil;
    }
    else if ([array isKindOfClass:[NSArray class]])
    {
        for(int i = 0; i < ((NSArray *)array).count; i++)
        {
            if ([array[i] isKindOfClass:[NSDictionary class]]) {
                NSArray *allKeys = [array[i] allKeys];
                for(int j = 0; j < allKeys.count;j++)
                {
                    if([allKeys[j] isEqual:key])
                        return [array[i] objectForKey:key];
                }
            }
            else if([array[i] isKindOfClass:[NSArray class]])
                return [ClinicalCollections findNodeWithKey:key forArray:array[i]];
        }
    }
    return nil;
}

+ (NSArray *) getValueArrayFromCollections:(NSDictionary *) clinicalCollections usingLevelKey:(NSArray *) levelKeys forKey:keyName
{
    
    NSArray *results = [clinicalCollections objectForKey:RESULTS_KEY];
    
    NSMutableArray *valueArray1 = [[NSMutableArray alloc] initWithArray:results];
    NSMutableArray *valueArray2 = [[NSMutableArray alloc] initWithCapacity:CAPACITY_HINT];
    for(NSString *levelKey in levelKeys) {
        for(int i = 0; i < valueArray1.count;i++)
            if([ClinicalCollections findNodeWithKey:levelKey forArray:valueArray1[i]])
                [valueArray2 addObject:[ClinicalCollections findNodeWithKey:levelKey forArray:valueArray1[i]]];
        valueArray1 = [valueArray2 mutableCopy];
        [valueArray2 removeAllObjects];
    }
    
    for(int i = 0; i < valueArray1.count;i++)
        if([ClinicalCollections findNodeWithKey:keyName forArray:valueArray1[i]])
            [valueArray2 addObject:[ClinicalCollections findNodeWithKey:keyName forArray:valueArray1[i]]];
    return valueArray2;
}

+ (NSDictionary *) clinicalCollectionsQueryfetchWithLimit:(int) limit usingParameters:(NSDictionary *) parameters selectingFields:(NSArray *) fields
{
    NSString *url;
    NSString *params;
    NSString *flds;
    NSArray *keys = [parameters allKeys];
    
    for (int i = 0; i < keys.count; i++)
        if(!i) params = [NSString stringWithFormat:@"%@:%@" , keys[i], [parameters objectForKey:keys[i]]];
        else params = [NSString stringWithFormat:@"%@,%@:%@" , params, keys[i], [parameters objectForKey:keys[i]]];
    
    for (int i = 0; i < fields.count; i++)
        if(!i) flds = [NSString stringWithFormat:@"%@%@" , API_FIELD_PARAMS , [fields objectAtIndex:i]];
        else flds = [NSString stringWithFormat:@"%@,%@" , flds, [fields objectAtIndex:i]];
    
    url = [NSString stringWithFormat:@"%@%@%@&limit=%d", API_BASE_URL , params, flds, (limit == -1) ? DEFAULT_FETCH_LIMIT:limit];
    NSLog(@"URL = %@" , url);
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSData *jsonData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:nil] dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    
    NSDictionary *clinicalCollections = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error] : nil;
    
    return clinicalCollections;
}
+ (NSDictionary *) clinicalCollectionsQueryExhibitfetchWithLimit:(int) limit usingParameters:(NSDictionary *) parameters selectingFields:(NSArray *) fields
{
    NSString *url;
    NSString *params;
    NSString *flds;
    NSArray *keys = [parameters allKeys];
    
    for (int i = 0; i < keys.count; i++)
        if(!i) params = [NSString stringWithFormat:@"%@:%@" , keys[i], [parameters objectForKey:keys[i]]];
        else params = [NSString stringWithFormat:@"%@,%@:%@" , params, keys[i], [parameters objectForKey:keys[i]]];
    
    for (int i = 0; i < fields.count; i++)
        if(!i) flds = [NSString stringWithFormat:@"%@%@" , API_FIELD_PARAMS , [fields objectAtIndex:i]];
        else flds = [NSString stringWithFormat:@"%@,%@" , flds, [fields objectAtIndex:i]];
    
    url = [NSString stringWithFormat:@"%@%@%@&limit=%d", API_EXHIBIT_URL , params, flds, (limit == -1) ? DEFAULT_FETCH_LIMIT:limit];
    NSLog(@"X-URL = %@" , url);
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSData *jsonData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:nil] dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    
    NSDictionary *clinicalCollectionsExhibit = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error] : nil;
    
    return clinicalCollectionsExhibit;
}

@end
